/*
 * TowerRedOne.java
 *
 * Created on December 9, 2007, 2:13 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package hextd.tower;

import hextd.hex.Hex;
import hextd.creep.Creep;
import hextd.core.Context;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

/**
 *
 * @author hylke
 */
public class TowerRedOne extends TowerBase {

	public static int price = 200;
	public static int damage = 35;
	public static float range = 5f;
	public static float spreadRadiusBase = 2.25f;
	//
	private Creep fireAt = null;
	private float enemyX = 0;
	private float enemyY = 0;
	private Creep.COLOR damageColor = Creep.COLOR.RED;
	private int coolDown = 0;
	private int coolDownMax = 4;
	//
	private Color transColor;
	private Shape spread;
	private float spreadRadius;
	//
	private Creep[] currentTargets;

	/** Creates a new instance of TowerRedOne */
	public TowerRedOne(Context context, Hex hex, int owner, int activateTick) {
		super(TowerFactory.type.redOne, price, damage, range, owner, activateTick);
		this.lineColor = Color.RED;
		hexInfluence = 1;
		this.transColor = new Color(this.lineColor.getRed(), this.lineColor.getGreen(), this.lineColor.getBlue(), 75);
		this.doInit(context, hex);
	}

	@Override
	public void runTick(int gameTime) {
		if (coolDown > 0) {
			coolDown--;
			//if (coolDown > 2) {
			//	float radius = paintScale * spreadRadius;
			//	spread = new Ellipse2D.Float(fireAt.getPaintX() - radius, fireAt.getPaintY() - radius, radius * 2, radius * 2);
			//}
		} else {
			if (!this.lock) {
				this.fireAt = null;
			}
			Creep enemy = this.findEnemy(this.fireAt, false);
			this.fireAt = enemy;

			if (enemy != null) {
				float dx, dy, r;
				long splashDamage;

				enemyX = fireAt.getX();
				enemyY = fireAt.getY();
				currentTargets = findEnemiesInRange(enemyX, enemyY, spreadRadius);
				Creep enemy2;
				for (int i = 0; i < currentTargets.length; i++) {
					enemy2 = currentTargets[i];
					dx = enemyX - enemy2.getX();
					dy = enemyY - enemy2.getY();
					r = (float) Math.sqrt(dx * dx + dy * dy);
					splashDamage = Math.round(damageCurrent * (1 - r / spreadRadius));
					enemy2.doDamage(getOwner(), splashDamage, damageColor);
				}

				//System.out.println("TR! fire at tick: "+gameTime);
				coolDown = coolDownMax;
				//float radius = paintScale * spreadRadius;
				//spread = new Ellipse2D.Float(fireAt.getPaintX() - radius, this.fireAt.getPaintY() - radius, radius * 2, radius * 2);
			}
		}
	}

	@Override
	public String getStatusString() {
		String retString = "";
		retString += "<h1>Red Tower 1</h1>" +
				"<p>" +
				"<b>Level:</b> " + (1 + this.level) + "<br>\n" +
				"<b>Damage:</b> " + this.damageText + "<br>\n" +
				"<b>Range:</b> " + this.rangeText + "<br>" +
				"</p>\n";
		return retString;
	}

	@Override
	public String getInfoString() {
		String retString = "";
		retString += "<h1>Red Tower 1</h1>" +
				"<p>" +
				"Tower with one laser. " +
				"The impact of this laser does splash damage to all enemies surrounding the target." +
				"<br>Strong against red enemies, very weak against green enemies." +
				"</p>" +
				"<p>" +
				"<b>Price:</b> " + TowerRedOne.price + "<br>" +
				"<b>Damage:</b> " + this.damageCurrent + " + splash<br>" +
				"<b>Range:</b> " + TowerRedOne.form.format(rangeCurrent) + "<br>" +
				"<b>Refire rate:</b> 4/s" +
				"</p>";
		return retString;
	}

	@Override
	protected void calcDamageRange() {
		super.calcDamageRange();
		this.spreadRadius = TowerRedOne.spreadRadiusBase;
	}

	@Override
	protected void generateShapes() {
		int shapeCount = 3;
		this.shapes = new Shape[shapeCount];
		this.shapes[0] = new Ellipse2D.Float(-0.5f * this.paintScale, -0.5f * this.paintScale, 1.0f * this.paintScale, 1.0f * this.paintScale);
		this.shapes[1] = new Ellipse2D.Float(-0.5f * this.paintScale, -0.5f * this.paintScale, 1.0f * this.paintScale, 1.0f * this.paintScale);
		this.shapes[2] = new Ellipse2D.Float(-1.0f * strokeScale, -1.0f * strokeScale, 2.0f * strokeScale, 2.0f * strokeScale);

		this.shapeAction = new int[shapeCount];
		this.shapeAction[0] = 2;
		this.shapeAction[1] = 1;
		this.shapeAction[2] = 1;

		this.shapeColor = new Color[shapeCount];
		this.shapeColor[0] = this.transColor;
		this.shapeColor[1] = this.lineColor;
		this.shapeColor[2] = this.lineColor;

		this.strokes = new Stroke[shapeCount];
		this.strokes[0] = new BasicStroke(2.0f * strokeScale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
		this.strokes[1] = this.strokes[0];
		this.strokes[2] = this.strokes[0];
	}

	@Override
	public void paintEffects(Graphics2D g2, int gameTime) {
		Stroke defaultStroke = g2.getStroke();

		if (fireAt != null && coolDown == coolDownMax) {
			g2.setColor(lineColor);
			float ex = this.fireAt.getPaintX() + ((float) (Math.random() - 0.5) * paintScale / 10f);
			float ey = this.fireAt.getPaintY() + ((float) (Math.random() - 0.5) * paintScale / 10f);
			g2.draw(new Line2D.Float(this.xPaint, this.yPaint, ex, ey));
			for (int i = 0; i < this.currentTargets.length; i++) {
				g2.draw(new Line2D.Float(ex, ey, currentTargets[i].getPaintX(), currentTargets[i].getPaintY()));
			}
		}
		//if (this.coolDown > this.coolDownMax - 1) {
		//	g2.setColor(transColor);
		//	g2.draw(spread);
		//}

		g2.setStroke(defaultStroke);
	}
}
